﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class ThemSanPhamDMH_CT : Form
    {
        public string iddmh;
        public string ngayMH;

        public ThemSanPhamDMH_CT(string iddmh, string ngayMH)
        {
            InitializeComponent();
            this.iddmh = iddmh;
            this.ngayMH = ngayMH;
        }

        private void ThemSanPhamDMH_CT_Load(object sender, EventArgs e)
        {
            Load_LoaiSanPham();
            Load_SanPham(comboBoxLoaiSP.SelectedValue.ToString());
        }

        private void comboBoxLoaiSP_SelectedIndexChanged(object sender, EventArgs e)
        {
            Load_SanPham(comboBoxLoaiSP.SelectedValue.ToString());
        }

        private void Load_SanPham(string loai)
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionString = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select * from SanPham where MaSo_LSP = @loai";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@loai", loai);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "SanPham");
            DataTable dt = ds.Tables["SanPham"];

            comboBoxTenSP.DisplayMember = "Ten_SP";
            comboBoxTenSP.ValueMember = "MaSo_SP";
            comboBoxTenSP.DataSource = ds.Tables["SanPham"];
        }

        private void Load_LoaiSanPham()
        {
            Config file = new Config(Application.StartupPath + "//Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from LoaiSanPham";
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "LoaiSanPham");
            DataTable dt = ds.Tables["LoaiSanPham"];

            comboBoxLoaiSP.DisplayMember = "Ten_LSP";
            comboBoxLoaiSP.ValueMember = "MaSo_LSP";
            comboBoxLoaiSP.DataSource = ds.Tables["LoaiSanPham"];
        }

        private void ButttonThemSanPhamDMH_CT_Click(object sender, EventArgs e)
        {
            labelThongBaoThemSP.Text = "";
            if (textBoxSoLuong.Text == "" || textBoxGiaMua.Text == "")
                labelThongBaoThemSP.Text = "Xin nhập đầy đủ dữ liệu";
            else
            {
                Config file = new Config(Application.StartupPath + "\\Config.ini");
                string connectionString = file.ReadValue("server", "con");
                SqlConnection conn = new SqlConnection(connectionString);
                string sql = "select * from DonMuaHang_CT where MaSo_DMH = @iddmh and MaSo_SP = @idsp";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@iddmh", iddmh);
                cmd.Parameters.AddWithValue("@idsp", comboBoxTenSP.SelectedValue);

                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    labelThongBaoThemSP.Text = "Mã số sản phẩm đã tồn tại";
                    reader.Close();
                }
                else
                {
                    reader.Close();
                    if ((MessageBox.Show("Bạn có muốn thêm dữ liệu không?", "Thông Báo", MessageBoxButtons.OKCancel)) == DialogResult.OK)
                    {
                        labelThongBaoThemSP.Text = "Đã thêm dữ liệu thành công";
                        this.DialogResult = DialogResult.OK;

                        cmd.Parameters.Clear();
                        cmd.CommandText = "insert into DonMuaHang_CT(MaSo_DMH, MaSo_SP, SoLuong, GiaMua) values (@iddmh, @idsp, @sl, @gm)";
                        cmd.Parameters.AddWithValue("@iddmh", iddmh);
                        cmd.Parameters.AddWithValue("@idsp", comboBoxTenSP.SelectedValue);
                        cmd.Parameters.AddWithValue("@sl", textBoxSoLuong.Text);
                        cmd.Parameters.AddWithValue("@gm", textBoxGiaMua.Text);
                        cmd.ExecuteNonQuery();
                    }
                }
                conn.Close();
            }     
        }

        private void buttonThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
